package foundation.pcmUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

import foundation.fileUtil.XMLFileUtil;


public class SystemModelHelper {	
	
	private XMLFileUtil xmlFileHelper;
	private String desSysFileName;
	
	public SystemModelHelper(){
		
	}

	public SystemModelHelper(String desSysFileName){
		this.desSysFileName = desSysFileName;	
		File sysModFile = new File(desSysFileName);
		xmlFileHelper = new XMLFileUtil(sysModFile);
	}
	
	public List<Element> readNode(String pathName) throws JDOMException{
		return xmlFileHelper.readNode(pathName);
	}
	public List<Element> readNode(String pathName,String prefix,String url) throws JDOMException {
		return xmlFileHelper.readNode(pathName, prefix, url);
	}
	
	public synchronized void write() throws FileNotFoundException, IOException{
		xmlFileHelper.write(desSysFileName);
	}
	
	public void close(){
		xmlFileHelper.close(desSysFileName);
	}
	/**
	 * 根据当前实体名，在system模型文件中找到对应组件的ID
	 * @param entityName
	 * @return
	 */
	public String getCompIDByEntity(String entityName,String prefix){
		String curCompID = null;
		String path = "/system:System/assemblyContexts__ComposedStructure[@entityName='" + entityName+ "']";
		try {
			Element compLink = xmlFileHelper.readNode(path,"system",
					"http://palladiosimulator.org/PalladioComponentModel/System/5.1").get(0);			
			Element encapsulatedComponent = compLink.getChildren().get(0);			
			String compLinkID = encapsulatedComponent.getAttributeValue("href");
			curCompID = compLinkID.replace(prefix+".repository#", "");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return curCompID;
		
	}
	
	/**
	 * 在system模型中，根据组件的entityName，修改href
	 * @param entityName
	 * @param href
	 */
	public void mdfyComp(String entityName, String href) {	
		String path = "/system:System/assemblyContexts__ComposedStructure[@entityName='" + entityName+ "']";
		Element compLink;
		try {
			compLink = xmlFileHelper.readNode(path,"system",
					"http://palladiosimulator.org/PalladioComponentModel/System/5.1").get(0);
			Element encapsulatedComponent = compLink.getChildren().get(0);
			encapsulatedComponent.setAttribute("href",href);
			
			write();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	/**
	 * 在system模型中，根据请求角色的entityName，修改href
	 * @param entityName
	 * @param href
	 * 
	 */
	public void mdfyRequiredRole(String entityName, String href) {
		String path = "/system:System/connectors__ComposedStructure[@entityName='"
				+ entityName + "']/" + "requiredRole_AssemblyConnector";

		Element requiredRole;
		try {
			requiredRole = xmlFileHelper.readNode(path, "system",
							"http://palladiosimulator.org/PalladioComponentModel/System/5.1").get(0);
			requiredRole.setAttribute("href", href);
			
			write();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();		
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 在system模型中，根据提供角色的entityName，修改href
	 * @param entityName
	 * @param href
	 * @param rltPath
	 */
	public void mdfyProvidedRole(String entityName, String href) {
		String path = "/system:System/connectors__ComposedStructure[@entityName='"
				+ entityName + "']/providedRole_AssemblyConnector";
		Element providedRole;
		try {
			providedRole = xmlFileHelper.readNode(path, "system",
						"http://palladiosimulator.org/PalladioComponentModel/System/5.1").get(0);
			providedRole.setAttribute("href", href);
			
			write();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();					
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 在system模型中，根据WebServer的提供角色的entityName，修改href
	 * @param entityName
	 * @param href
	 */
	public void mdfyProvidedDelegRole(String entityName, String href) {
		String path = "/system:System/connectors__ComposedStructure[@entityName='"
				+ entityName + "']/" + "innerProvidedRole_ProvidedDelegationConnector";
		Element providedRole;
		try {
			providedRole = xmlFileHelper.readNode(path, "system",
						"http://palladiosimulator.org/PalladioComponentModel/System/5.1").get(0);
			providedRole.setAttribute("href", href);
			
			write();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();					
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public XMLFileUtil getXmlFileHelper() {
		return xmlFileHelper;
	}

	public void setXmlFileHelper(XMLFileUtil xmlFileHelper) {
		this.xmlFileHelper = xmlFileHelper;
	}

	public String getDesSysFileName() {
		return desSysFileName;
	}

	public void setDesSysFileName(String desSysFileName) {
		this.desSysFileName = desSysFileName;
	}	
	
	public String getSysIDByHref(String compID){
		String sysID = null;
		String href = "brs.repository#"+compID;
		String path = "/system:System/assemblyContexts__ComposedStructure/"
				+ "encapsulatedComponent__AssemblyContext[@href='" + href+ "']";
		try {
			Element sysIDElme = xmlFileHelper.readNode(path,"system",
					"http://palladiosimulator.org/PalladioComponentModel/System/5.1").get(0);
			sysID = sysIDElme.getParentElement().getAttributeValue("id");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sysID;	
	}
	
	public List<String> getCompIDs(List<String> compEntyIDs){
		List<String> compIDs = new ArrayList<String>();
		try {
			for (String compEntyID : compEntyIDs) {
				String path = "/system:System/"
						+ "assemblyContexts__ComposedStructure[@id='"
						+ compEntyID + "']/"
						+ "encapsulatedComponent__AssemblyContext";
				Element compIDElme = xmlFileHelper
						.readNode(path, "system",
								"http://palladiosimulator.org/PalladioComponentModel/System/5.1")
						.get(0);
				String compIDLink = compIDElme.getAttributeValue("href");
				String compID = compIDLink.replace("brs.repository#", "");
				compIDs.add(compID);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return compIDs;
	}
}
